package com.xnyzc.lhy.order.service.impl.vehicle;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.order.entity.vehicle.OaCVehicleAccident;
import com.xnyzc.lhy.order.entity.vehicle.OaSysVehicle;
import com.xnyzc.lhy.order.mapper.vehicle.OaCVehicleAccidentMapper;
import com.xnyzc.lhy.order.mapper.vehicle.OaSysVehicleMapper;
import com.xnyzc.lhy.order.service.vehicle.IOaCVehicleAccidentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车辆事故表 服务实现类
 * </p>
 *
 * @author lhy@xnyzc.com
 * @since 2019-07-24
 */
@Service
public class OaCVehicleAccidentServiceImpl extends ServiceImpl<OaCVehicleAccidentMapper, OaCVehicleAccident> implements IOaCVehicleAccidentService {

    @Autowired
    private OaCVehicleAccidentMapper vehicleAccidentMapper;

    @Autowired
    private OaSysVehicleMapper vehicleMapper;


    /**
     * 添加事故信息
     *
     * @param vehicleAccident 事故实体类
     */
    @Override
    public Rv addVehicleAccident(OaCVehicleAccident vehicleAccident) {
        OaSysVehicle oaSysVehicle = vehicleMapper.selectById(vehicleAccident.getVehicleId());
        if (CheckUtil.objIsEmpty(oaSysVehicle)) {
            throw PangException.create(EErrorCode.argCheckErr);
        }
        vehicleAccident.setStatus(0);
        boolean isTrue = vehicleAccidentMapper.insert(vehicleAccident) > 0 ? true : false;
        return Rv.wrap(isTrue ? EErrorCode.success : EErrorCode.addError);
    }

    /**
     * 根据ID获取事故信息
     *
     * @param id 事故ID
     */
    @Override
    public Rv getVehicleAccident(Long id) {
        OaCVehicleAccident oaCVehicleAccident = vehicleAccidentMapper.selectById(id);
        if (CheckUtil.objIsEmpty(oaCVehicleAccident)) {
            throw PangException.create(EErrorCode.unData);
        }
        return Rv.wrap(EErrorCode.success, oaCVehicleAccident);
    }

    /**
     * 根据车辆ID查询列表数据
     *
     * @param vehicleId 车辆ID
     * @param page      当前页
     * @param limit     每页显示数量
     */
    @Override
    public Rv listVehicleAccident(Long vehicleId, Integer page, Integer limit) {
        OaSysVehicle oaSysVehicle = vehicleMapper.selectById(vehicleId);
        if (CheckUtil.objIsEmpty(oaSysVehicle)) {
            throw PangException.create(EErrorCode.unData);
        }
        //oaSysVehicle.getVehicleId()
        Qw qw = Qw.create();
        List<OaCVehicleAccident> oaCVehicleAccident = vehicleAccidentMapper.selectList(qw);

        //数据封装
        Map<String, Object> result = new HashMap<>();
        result.put("vehicle", oaSysVehicle);
        result.put("vehicleAccident", oaCVehicleAccident);
        return Rv.wrap(EErrorCode.success, result);
    }


}
